package com.wanglibao.woodtrade.woodtrade.ui.activity.user;

import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.config.NetConfig;
import com.wanglibao.woodtrade.woodtrade.jsonrpc.JsonRPCAsyncTask;
import com.wanglibao.woodtrade.woodtrade.model.User;
import com.wanglibao.woodtrade.woodtrade.model.impl.UserImpl;
import com.wanglibao.woodtrade.woodtrade.ui.activity.BaseActivity;
import com.wanglibao.woodtrade.woodtrade.util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ModifyPasswordActivity extends BaseActivity {

    @Bind(R.id.rl_common_title)
    RelativeLayout rl_common_title;

    @Bind(R.id.edit_activity_modify_password_new_password)
    EditText editActivityModifyPasswordNewPassword;

    @Bind(R.id.edit_activity_modify_password_repeat_password)
    EditText editActivityModifyPasswordRepeatPassword;

    @Bind(R.id.sv_activity_modify_password_view)
    ScrollView sv_activity_modify_password_view;

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.arg2 != 2) {
                String password = editActivityModifyPasswordRepeatPassword.getText().toString();
                UserImpl userImpl = new UserImpl(ModifyPasswordActivity.this);
                User user = userImpl.getUser();
                user.password = password;
                userImpl.update(user);
                onBackPressed();
            }
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_password);
        ButterKnife.bind(this);

        if (Build.VERSION.SDK_INT >= 19) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        } else {
            rl_common_title.setPadding(0, 0, 0, 0);
        }
        initView();
    }

    private void initView() {
        addViewClicklistener(tvBack, tvRight);
        setRightText("保存");
        tvTitle.setText("修改密码");

        if (Build.VERSION.SDK_INT >= 19) {
            final View decorView = getWindow().getDecorView();
            decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    Rect rect = new Rect();
                    decorView.getWindowVisibleDisplayFrame(rect);
                    int screenHeight = decorView.getRootView().getHeight();
                    int height = screenHeight - rect.bottom;
                    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) sv_activity_modify_password_view.getLayoutParams();
                    layoutParams.setMargins(0, 0, 0, height);
                    sv_activity_modify_password_view.requestLayout();
                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.tv_commonTitle_right) {
            String password = editActivityModifyPasswordNewPassword.getText().toString();
            String repeatPassword = editActivityModifyPasswordRepeatPassword.getText().toString();
            if (!TextUtils.isEmpty(password) && !TextUtils.isEmpty(repeatPassword)) {
                if (password.equals(repeatPassword)){
                    ToastUtil.createLongToast(this,"新密码不能与旧密码相同");
                }else{
                    JSONObject jsonObject = new JSONObject();
                    try {
                        jsonObject.put("token", new UserImpl(this).getUser().token);
                        jsonObject.put("old_password", password);
                        jsonObject.put("new_password", repeatPassword);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    JsonRPCAsyncTask jsonRPCAsyncTask = new JsonRPCAsyncTask(this, NetConfig.SERVER + NetConfig.TYPE_USER,
                            NetConfig.resetPassword, handler, "jsonObject", NetConfig.TaskId.resetPassword,
                            new Object[]{jsonObject});
                    jsonRPCAsyncTask.execute(new Void[]{});
                }
            } else {
                ToastUtil.createLongToast(this, "资料不完整");
            }
        }
    }
}
